﻿Imports MongoDB.Bson
Imports MongoDB.Driver
Imports MongoDB.Driver.Builders

Public Class DatabaseCalls

    Public Shared Function GetTerritoryByID(ByVal objObjectID As ObjectId) As Trebuchet.Territory

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcTerritories As MongoCollection(Of Trebuchet.Territory) = Nothing
        Dim mTerritory As Trebuchet.Territory = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcTerritories = mdDatabase.GetCollection(Of Trebuchet.Territory)("Territories")

            mTerritory = mcTerritories.FindOneByIdAs(Of Trebuchet.Territory)(objObjectID)

            Return mTerritory

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetAllTerritoriesFromTerritoryTerritoryXrefByTerritoryID(ByVal objObjectID As ObjectId) As MongoCursor

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcTerritoryXref As MongoCollection(Of Trebuchet.TerritoryTerritoryXREF) = Nothing
        Dim mcTerritories As MongoCollection(Of Trebuchet.Territory) = Nothing
        Dim mcrTerritoryXref As Mongocursor = Nothing
        Dim mcrTerritories As Mongocursor = Nothing
        Dim baArray As BsonArray = Nothing
        Dim ttxItem As Trebuchet.TerritoryTerritoryXREF = Nothing
        Dim bvVlaue As BsonValue = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcTerritoryXref = mdDatabase.GetCollection(Of Trebuchet.TerritoryTerritoryXREF)("TerritoryTerritoryXref")
            mcTerritories = mdDatabase.GetCollection(Of Trebuchet.Territory)("Territories")

            'get all active records from database
            mcrTerritoryXref = mcTerritoryXref.FindAs(Of Trebuchet.TerritoryTerritoryXREF)(Query.EQ("FromTerritory_id", objObjectID))
            baArray = New BsonArray()

            For Each ttxItem In mcrTerritoryXref

                bvVlaue = ttxItem.ToTerritory_id

                baArray.Add(bvVlaue)

            Next

            mcrTerritories = mcTerritories.FindAs(Of Trebuchet.Territory)(Query.In("_id", baArray))

            Return mcrTerritories

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetAllTerritories(ByVal objMapId As ObjectId) As MongoCursor

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcTerritories As MongoCollection(Of Trebuchet.Territory) = Nothing
        Dim mcrcTerritories As MongoCursor = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcTerritories = mdDatabase.GetCollection(Of Trebuchet.Territory)("Territories")

            mcrcTerritories = mcTerritories.FindAs(Of Trebuchet.Territory)(Query.EQ("Map_id", objMapId))

            Return mcrcTerritories

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetAllMaps() As MongoCursor

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcMaps As MongoCollection(Of Trebuchet.Map) = Nothing
        Dim mcMapCursor As MongoCursor = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcMaps = mdDatabase.GetCollection(Of Trebuchet.Map)("Maps")

            'get all active maps from database
            mcMapCursor = mcMaps.FindAllAs(Of Trebuchet.Map)()

            Return mcMapCursor

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetAllNonSelectedTerritoriesForXref(ByVal baSelected As BsonArray) As MongoCursor

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcTerritories As MongoCollection(Of Trebuchet.Territory) = Nothing
        Dim mcrTerritories As Mongocursor = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcTerritories = mdDatabase.GetCollection(Of Trebuchet.Territory)("Territories")

            mcrTerritories = mcTerritories.FindAs(Of Trebuchet.Territory)(Query.NotIn("_id", baSelected))

            Return mcrTerritories

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetAllMapGroups(ByVal objMapId As ObjectId) As MongoCursor

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcMapGroups As MongoCollection(Of Trebuchet.MapGroup) = Nothing
        Dim mcrcMapGroups As MongoCursor = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcMapGroups = mdDatabase.GetCollection(Of Trebuchet.MapGroup)("MapGroups")

            mcrcMapGroups = mcMapGroups.FindAs(Of Trebuchet.MapGroup)(Query.EQ("Map_id", objMapId))

            Return mcrcMapGroups

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetMapGroupByID(ByVal objObjectID As ObjectId) As Trebuchet.MapGroup

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcMapGroups As MongoCollection(Of Trebuchet.MapGroup) = Nothing
        Dim mMapGroup As Trebuchet.MapGroup = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcMapGroups = mdDatabase.GetCollection(Of Trebuchet.MapGroup)("MapGroups")

            mMapGroup = mcMapGroups.FindOneByIdAs(Of Trebuchet.MapGroup)(objObjectID)

            Return mMapGroup

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetAllTerritoriesFromMapGroupTerritoryXrefByMapGroupID(ByVal objObjectID As ObjectId) As MongoCursor

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcMapGroupTerritoryXREF As MongoCollection(Of Trebuchet.MapGroupTerritoryXREF) = Nothing
        Dim mcTerritories As MongoCollection(Of Trebuchet.Territory) = Nothing
        Dim mcrMapgroupTerritoryXref As Mongocursor = Nothing
        Dim mcrTerritories As Mongocursor = Nothing
        Dim baArray As BsonArray = Nothing
        Dim ttxItem As Trebuchet.MapGroupTerritoryXREF = Nothing
        Dim bvVlaue As BsonValue = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcMapGroupTerritoryXREF = mdDatabase.GetCollection(Of Trebuchet.MapGroupTerritoryXREF)("MapGroupTerritoryXREF")
            mcTerritories = mdDatabase.GetCollection(Of Trebuchet.Territory)("Territories")

            'get all active records from database
            mcrMapgroupTerritoryXref = mcMapGroupTerritoryXREF.FindAs(Of Trebuchet.MapGroupTerritoryXREF)(Query.EQ("MapGroup_id", objObjectID))
            baArray = New BsonArray()

            For Each ttxItem In mcrMapgroupTerritoryXref

                bvVlaue = ttxItem.Territory_id

                baArray.Add(bvVlaue)

            Next

            mcrTerritories = mcTerritories.FindAs(Of Trebuchet.Territory)(Query.In("_id", baArray))

            Return mcrTerritories

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetMapByID(ByVal objObjectID As ObjectId) As Trebuchet.Map

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcMaps As MongoCollection(Of Trebuchet.Map) = Nothing
        Dim mMap As Trebuchet.Map = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcMaps = mdDatabase.GetCollection(Of Trebuchet.Map)("Maps")

            mMap = mcMaps.FindOneByIdAs(Of Trebuchet.Map)(objObjectID)

            Return mMap

        Catch ex As Exception

        End Try

    End Function

    Public Shared Function GetUserByID(ByVal objObjectID As ObjectId) As Trebuchet.User

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUserSettings As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mUserSettings As Trebuchet.User = Nothing

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcUserSettings = mdDatabase.GetCollection(Of Trebuchet.User)("Users")

            mUserSettings = mcUserSettings.FindOneByIdAs(Of Trebuchet.User)(objObjectID)

            Return mUserSettings

        Catch ex As Exception

        Finally

            GetUserByID = mUserSettings

        End Try

    End Function

    Public Shared Function IsFacebookUser(ByVal objObjectID As ObjectId) As Boolean

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUserSettings As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mUserSettings As Trebuchet.User = Nothing
        Dim bolIsFacebookUser As Boolean

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcUserSettings = mdDatabase.GetCollection(Of Trebuchet.User)("Users")

            mUserSettings = mcUserSettings.FindOneByIdAs(Of Trebuchet.User)(objObjectID)

            If mUserSettings.FacebookID.Length <> 0 Then

                bolIsFacebookUser = True

            Else

                bolIsFacebookUser = False

            End If

        Catch ex As Exception

        Finally

            IsFacebookUser = bolIsFacebookUser

        End Try

    End Function

    Public Shared Function PasswordChanged(ByVal objObjectID As ObjectId, _
                                           ByVal strCurrentPassword As String, _
                                           ByVal strNewPassword As String, _
                                           ByVal strConfirmPassword As String) As Boolean

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUserSettings As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mUserSettings As Trebuchet.User = Nothing
        Dim bolPasswordChanged As Boolean

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcUserSettings = mdDatabase.GetCollection(Of Trebuchet.User)("Users")

            mUserSettings = mcUserSettings.FindOneByIdAs(Of Trebuchet.User)(objObjectID)

            'Validate current password
            If strCurrentPassword = Encryption.AES_Decrypt(mUserSettings.Password, Encryption.strSharedAESPassword) Then

                'Make sure New password and Confirm password are the same
                If strNewPassword = strConfirmPassword Then

                    mUserSettings.Password = Encryption.AES_Encrypt(strNewPassword.Trim, Encryption.strSharedAESPassword)

                    mcUserSettings.Save(mUserSettings)

                    bolPasswordChanged = True

                Else

                    bolPasswordChanged = False

                End If

            Else

                bolPasswordChanged = False

            End If

        Catch ex As Exception

        Finally

            PasswordChanged = bolPasswordChanged

        End Try

    End Function
End Class
